package com.serv.common.route;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.jfinal.aop.Interceptor;
import com.jfinal.aop.Invocation;

public class CrossDomainInterceptor implements Interceptor {

	@Override
	public void intercept(Invocation inv) {
		HttpServletRequest req = inv.getController().getRequest();
		HttpServletResponse res = inv.getController().getResponse();
 
		//跨域
		res.setHeader("Access-Control-Allow-Origin", "*");
 
		//跨域 Header
		res.setHeader("Access-Control-Allow-Methods", "*");
 
		res.setHeader("Access-Control-Allow-Headers", "token,username,password,appid,appsecret");
 
		// 浏览器是会先发一次options请求，如果请求通过，则继续发送正式的post请求
		// 配置options的请求返回
		if (req.getMethod().equals("OPTIONS")) {
			res.setStatus(200);
			inv.getController().renderJson("OPTIONS returns OK");
            return;
		}
		
		inv.invoke();
	}
}
